package org.karelun.gef.dom;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.karelun.gef.editors.StateDiagramEditor;
import org.karelun.gef.helper.DOMConstant;
import org.karelun.gef.model.DiagramModel;
import org.karelun.gef.model.Node;
import org.karelun.gef.model.RealizeConnectionModel;
import org.karelun.gef.model.StateModel;
import org.karelun.gef.model.StateChoiceModel;
import org.karelun.gef.model.StateEndModel;
import org.karelun.gef.model.StateStartModel;
import org.karelun.gef.utils.DOMUtils;
import org.w3c.dom.Element;

public class ReadStateDiagram {
	private StateDiagramEditor editor;
	private Map<String,Node> map=new HashMap<String, Node>();
	
	public ReadStateDiagram(StateDiagramEditor editor){
		this.editor=editor;
	}
	public void readXML(Element rootEle){
		DiagramModel diagram = editor.getDiagram();
		Element[] children = DOMUtils.getChildElements(rootEle);
		for(Element ele:children){
			String type = ele.getNodeName();
			if(isNode(type)){
				Node model=null;
				if(DOMConstant.TAG_start.equals(type))
					model=new StateStartModel();
				else if(DOMConstant.TAG_state.equals(type))
					model=new StateModel();
				else if(DOMConstant.TAG_choice.equals(type))
					model=new StateChoiceModel();
				else if(DOMConstant.TAG_end.equals(type))
					model=new StateEndModel();
				
				if(model==null)
					continue;
				String name = ele.getAttribute(DOMConstant.ATTR_name);
				model.setText(name);
				String id=ele.getAttribute(DOMConstant.ATTR_id);
				model.setId(id);
				map.put(id, model);
				
				Element locEle = DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_loc)[0];
				String x = locEle.getAttribute(DOMConstant.ATTR_x);
				String y = locEle.getAttribute(DOMConstant.ATTR_y);
				model.setLocation(new Point(Integer.parseInt(x),Integer.parseInt(y)));
				
				Element sizeEle = DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_size)[0];
				String height = sizeEle.getAttribute(DOMConstant.ATTR_height);
				String width = sizeEle.getAttribute(DOMConstant.ATTR_width);
				model.setSize(new Dimension(Integer.parseInt(width),Integer.parseInt(height)));
				
				diagram.addNode(model);
			}else if(DOMConstant.TAG_connection.equals(type)){
				Element sourceEle=DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_source)[0];
				String sourceNodeId=sourceEle.getAttribute(DOMConstant.ATTR_sourceNode);
				if(map==null||map.isEmpty())
					return;
				Node sourceNode=map.get(sourceNodeId);
				Element targetEle=DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_target)[0];
				String targetNodeId=targetEle.getAttribute(DOMConstant.ATTR_targetNode);
				Node targetNode=map.get(targetNodeId);
				
				String conn=ele.getAttribute(DOMConstant.ATTR_type);
				if(DOMConstant.realize_connection.equals(conn)){
					RealizeConnectionModel connModel=new RealizeConnectionModel();
					connModel.setSource(sourceNode);
					connModel.setTarget(targetNode);
					sourceNode.addsourceConnection(connModel);
					targetNode.addtargetConnection(connModel);
				}
			}
		}
	}
	private boolean isNode(String type) {
		if(DOMConstant.TAG_start.equals(type))
			return true;
		else if(DOMConstant.TAG_state.equals(type))
			return true;
		else if(DOMConstant.TAG_choice.equals(type))
			return true;
		else if(DOMConstant.TAG_end.equals(type))
			return true;
		return false;
	}
}
